package com.cy.pj.common.web;

import java.time.LocalDateTime;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import com.cy.pj.common.exception.ServiceException;
/**
 * Spring Web模块提供的拦截器类型(HandlerInterceptor),通过这样的拦截器可以再SpringWeb模块后端的
 * Controller之前之后做一些预处理
 * @author private
 *
 */
public class TimeAccessInterceptor implements HandlerInterceptor {
	/**
	 * 此方法会在你的目标Controller执行之前执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		LocalDateTime now = LocalDateTime.now();//JDK8
		int hour = now.getHour();
		if(hour<9 || hour>=19) {
			throw new ServiceException("不在访问时间:9-19");
		}
		return true;//true表示请求放行,false表示请求到此结束(被拦截)			
	}
}
